package com.common.log;

import com.entity.sys.SysLog;
import com.service.sys.SysLogService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Arrays;

/**
 * 日志切面
 */
@Aspect
@Component
public class OperationAspect {

    private final Logger logger = LoggerFactory.getLogger(OperationAspect.class);

    @Resource
    private SysLogService logService;

    @Pointcut("@annotation(com.common.log.OperationLog)")
    public void pointCut(){};


    @After(value = "pointCut()")
    public void getLog(JoinPoint joinPoint) {
        SysLog sysLog = new SysLog();
        MethodSignature signature = (MethodSignature)joinPoint.getSignature();
        Method method = signature.getMethod();
        //通过该方法获得标注的自定义注解
        OperationLog operationLog = method.getAnnotation(OperationLog.class);
        System.out.println(operationLog.toString());
        sysLog.setOperationType(operationLog.operationType().type);
        sysLog.setOperationDescribe(operationLog.describe());
        sysLog.setPrimaryKey(operationLog.primaryKey());
        sysLog.setEntity(operationLog.entity().getName());

        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();//这个RequestContextHolder是Springmvc提供来获得请求的东西
        HttpServletRequest request = ((ServletRequestAttributes)requestAttributes).getRequest();
        // 记录下请求内容
        logger.info("################URL : " + request.getRequestURL().toString());
        sysLog.setUrl(request.getRequestURL().toString());
        logger.info("################HTTP_METHOD : " + request.getMethod());
        sysLog.setRequestMethod(request.getMethod());
        logger.info("################IP : " + request.getRemoteAddr());
        sysLog.setIp(request.getRemoteAddr());

        Object[] args = joinPoint.getArgs();
        logger.info("################THE ARGS OF THE CONTROLLER : " + Arrays.toString(args));
        for (Object arg : args) {
            if( arg.getClass().getName().equals(operationLog.entity().getName())) {
                sysLog.setRequestData(arg.toString());
//                if(OperationType.INSERT.type.equals(operationLog.operationType().type)) {
//                    operationLog.entity().getClass() = (operationLog.entity()) arg;
//                    map.put("createBy", "admin");
//                }
            }
        }

        Object target = joinPoint.getTarget();
        System.out.println(target.getClass().getName());
//        String kind = joinPoint.getKind();
//        System.out.println(kind);

        logService.getBaseMapper().insert(sysLog);

    }
}
